"""
@File    :   route_depend.py
@Time    :   2024/03/28 23:35:28
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetTenantIdFromAccessTokenDependencies,
    GetUserNameFromAccessTokenDependencies,
)
from src.business.admin.system.settings.schemas.out import (
    SystemSettingsCompleteOutSchema,
)
from src.business.admin.system.settings.schemas.validate import (
    SystemSettingsUpdateValidateSchema,
)
from src.business.admin.system.settings.service import SystemSettingsService
from src.dependencies.no_introduction import depend_get_master_db_session


async def route_depend_get_system_settings(
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> SystemSettingsCompleteOutSchema:
    """查询单个租户系统配置的路由依赖

    Args:
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        SystemSettingsCompleteOutSchema: 信息
    """
    tenant_settings: SystemSettingsCompleteOutSchema = (
        await SystemSettingsService.get_system_settings_use_tenant_id(
            db_session, tenant_id
        )
    )
    return tenant_settings


async def route_depend_update_system_settings(
    update_schema: SystemSettingsUpdateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改租户系统配置的路由依赖

    Args:
        update_schema (TenantUpdateValidateSchema): 修改租户所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await SystemSettingsService.update_system_settings(
        db_session, tenant_id, update_schema, updater_username=username
    )
